
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }

    let operationList=[];
    let salesmanList=[];
    let operationPurchaseList=[];
    let billTypeData = [];
    let businessModelList = [];

    let deptOperationList=[];
    let deptSalesmanList=[];
    let deptOperationCodePurchaseList=[];
    layer.ready(function () {
        //供应商
        $.ajax({
            url: "customer2/getAllInspectUnit",
            success: function (res) {
                if (res.status == 200) {
                    $.each(res.data, function (index, item) {
                        $("#supplierCode").append(new Option(item.name, item.code));
                    });
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            operationList=getxmselect("#operationCode", "operationCode", userTree);
            operationPurchaseList=getxmselect("#operationCodePurchase", "operationCodePurchase", userTree);
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", userTree);
        } else {
            operationList=getxmselect("#operationCode", "operationCode", [{name: user.name, value: user.code}])
            operationPurchaseList=getxmselect("#operationCodePurchase", "operationCodePurchase", [{name: user.name, value: user.code}])
            salesmanList=getxmselect("#salesmanCode", "salesmanCode", [{name: user.name, value: user.code}])
        }

        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", deptTree);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
            deptOperationCodePurchaseList = getxmselect("#deptOperationCodePurchase", "deptOperationCodePurchase", deptTree);
        } else {
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", [{name: user.name, value: user.code}]);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
            deptOperationCodePurchaseList = getxmselect("#deptOperationCodePurchase", "deptOperationCodePurchase", [{name: user.name, value: user.code}]);
        }

        //单据信息
        let billNoStatus = [
            {name: "草稿箱", value: "1"},
            {name: "待批箱", value: "2"},
            {name: "已批箱", value: "3"},
            {name: "完成箱", value: "4"},
            {name: "垃圾箱", value: "5"},
            {name: "终止箱", value: "6"}
        ];
        billTypeData = getxmselect("#status", "status", billNoStatus);


        //获取业务模式信息
        $.ajax({
            url: "offer/getBusinessModel",
            async: false,
            success: function (data) {
                if (data.status == 200) {
                    let businessModelData = [];
                    for (let i in data.data) {
                        let tmp = {
                            name: data.data[i].name,
                            value: data.data[i].code
                        }
                        businessModelData.push(tmp);
                    }
                    businessModelList = getxmselect("#businessMode", "businessMode", businessModelData);
                    form.render('select');
                }
            }
        });

    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#offerselect'
        , data: initData
        , toolbar: '#offertoolbar'
        ,totalRow: true
        , cols: [[
            //以出运明细产品为主
            {field: 'numbers',width: 50,type:'numbers',title:'序号'}
            , {field: 'statusStr', title: '单据类型', sort: true, width: 120, align: 'center'}
            ,{field: 'invoiceCode', title: '发票号码', sort: true, width: 220}
            , {field: 'businessModelName', title: '业务模式', sort: true, width: 120}
            , {field: 'invoiceDateStr', title: '发票日期', sort: true, width: 120}
            , {field: 'inStockDateStr', title: '进仓装柜日期', sort: true, width: 120}
            , {field: 'actualDismissalTimeStr', title: '实际开船日期ATD', sort: true, width: 120}
            , {field: 'shippingStr', title: '出运日期', sort: true, width: 120}
            , {field: 'salesmanName', title: '业务员', sort: true, width: 120}
            , {field: 'operatorName', title: '出运操作员', sort: true, width: 120}
            , {field: 'operationNamePurchase', title: '采购操作员', sort: true, width: 120}
            , {field: 'productCode', title: '产品编码', sort: true, width: 120}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 120}
            , {field: 'supplierName', title: '供应商名称', sort: true, width: 120}
            , {field: 'salesContractNo', title: '外销合同号', sort: true, width: 220}
            , {field: 'signingDateStr', title: '外销签约日期', sort: true, width: 220}
            , {field: 'purchaseOrderNo', title: '采购单号', sort: true, width: 120}
            , {field: 'carryProductNum', title: '出运数量', sort: true, width: 120}
            , {field: 'purchaseProductNum', title: '采购数量', sort: true, width: 120}
            , {field: 'requestDeliveryDateStr', title: '要求交货日期', sort: true, width: 120}
            , {field: 'feedbackDeliveryDate', title: '供应商反馈交期', sort: true, width: 120}
            , {field: 'actualDeliveryDate', title: '实际交期', sort: true, width: 120}
            , {field: 'description', title: '反馈备注', sort: true, width: 120}
            , {field: 'proType', title: '产品类型', sort: true, width: 120}
        ]]
        , id: 'offertableid'
        , height: $(document).height() - $('#offerselect').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });

    form.on('submit(submitform)', function (data) {
        table.reload('offertableid', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , url: 'carryDetail/statistics/getShippingDetailByPage'
            , where: data.field
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"carryDetail/export/getShippingDetailByPageExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '供应商出货详情统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

    document.getElementById('clear').onclick = function(){
        operationList.setValue([ ]);
        salesmanList.setValue([ ]);
        operationPurchaseList.setValue([ ]);
        billTypeData.setValue([ ]);
        businessModelList.setValue([ ]);
        deptOperationCodePurchaseList.setValue([ ]);
        deptSalesmanList.setValue([ ]);
        deptOperationList.setValue([ ]);
    };
});